package com.ruoyi.web.controller.rjgc;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.ChatMessage;
import com.ruoyi.littleRedBook.service.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/chat")
@Api(tags = "聊天功能")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/send")
    @ApiOperation("发送消息")
    public AjaxResult sendMessage(@ApiParam("消息内容") @RequestBody ChatMessage message) {
        return chatService.sendMessage(message);
    }

    @GetMapping("/history")
    @ApiOperation("获取聊天记录（与指定用户）")
    public AjaxResult getChatHistory(
            @ApiParam("对方用户ID") @RequestParam Long otherUserId,
            @ApiParam("页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页条数，默认20") @RequestParam(defaultValue = "20") Integer pageSize) {
        return chatService.getChatHistory(otherUserId, pageNum, pageSize);
    }

    @PutMapping("/read/{otherUserId}")
    @ApiOperation("标记与指定用户的未读消息为已读")
    public AjaxResult markMessagesAsRead(@ApiParam("对方用户ID") @PathVariable Long otherUserId) {
        return chatService.markMessagesAsRead(otherUserId);
    }

    @GetMapping("/unread/count")
    @ApiOperation("获取与指定用户的未读消息数量")
    public AjaxResult getUnreadCount(@ApiParam("对方用户ID") @RequestParam Long otherUserId) {
        return chatService.getUnreadCount(otherUserId);
    }

    @GetMapping("/contacts")
    @ApiOperation("获取最近聊天联系人列表")
    public AjaxResult getContacts() {
        return chatService.getContacts();
    }
}
